<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@include file="/common/taglibs.jsp"%>
<HTML>
<o:MultiLanguage>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<META HTTP-EQUIV="pragma" CONTENT="no-cache"> 
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate"> 
<META HTTP-EQUIV="expires" CONTENT="0">
<link href="../css/dialog.css" rel="stylesheet" type="text/css" />
<STYLE type=text/css>
#tb td{text-align:center;}
</STYLE>

<script src='<ww:url value="/dwr/interface/ApplicationUtil.js"/>'></script>
<script src='<ww:url value="/dwr/interface/Sequence.js"/>'></script>
<script language=JavaScript src="../../dialog/sequence.js"></script>
<script language=JavaScript src="../../dialog/dialog.js"></script>
<script language=JavaScript src="../../dialog/script.js"></script>
<script src="../../dialog/common/fck_dialog_common.js" type="text/javascript"></script>
<link href="../../css/dialog.css" rel="stylesheet" type="text/css" />

<SCRIPT language=JavaScript>
var dialog	= window.parent ;
var oEditor = dialog.InnerDialogLoaded();
// Gets the document DOM
var oDOM = oEditor.FCK.EditorDocument;
var oActiveEl = dialog.Selection.GetSelectedElement() ;

function cardClick(cardID){
	var obj;
	for (var i=1;i<3;i++){
		obj=document.all("card"+i);
		obj.style.backgroundColor="#3A6EA5";
		obj.style.color="#FFFFFF";
	}
	
	obj=document.all("card"+cardID);
	obj.style.backgroundColor="#FFFFFF";
	obj.style.color="#3A6EA5";

	for (var i=1;i<3;i++){
		obj=document.all("content"+i);
		obj.style.display="none";
	}
	obj=document.all("content"+cardID);
	obj.style.display="";
}

function getCreateNamedElement() {
	var className="cn.myapps.core.dynaform.form.ejb.TabField";
	var id = oActiveEl ? oActiveEl.id: getFieldId();
	
	//alert(createRelStr());
	
	return CreateNamedElement(
		oEditor, oActiveEl, 'img', {
		src:"plugins/tabfield/tabfield.gif",
		type:"tabfield",
		classname: className,
		id: id,
		relStr: createRelStr(),
		showMode: temp.showMode.value,
		openAll: temp.openAll.value,
		selectedScript: HTMLEncode(temp.selectedScript.value)
	});
}

// 初始值
function InitDocument(){
	// 修改状态时取值
	try{
		if (oActiveEl){
		   	var relStr = oActiveEl.relStr;
		   	
			var datas = parseRelStr(relStr);
			addRows(datas,relStr);
			temp.showMode.value = HTMLDencode(oActiveEl.showMode);
			temp.openAll.value= HTMLDencode(oActiveEl.openAll);
		} else {
			addRows();
		}
	}catch(ex){}
	
	dialog.SetOkButton( true ) ;
	dialog.SetAutoSize( true ) ;
	//resize();
    if(document.getElementById("showMode").value==1){
        if(HTMLDencode(oActiveEl.openAll)=="true"){
        	document.getElementById("open_All").checked=true;
        }else{
        	document.getElementById("open_All").checked=false;
        }
    	document.getElementById("showSpan").style.display="inline";
    }else{
    	document.getElementById("open_All").checked=false;
    	document.getElementById("open_All").value="false";
    }

    if(oActiveEl!=null){
        temp.selectedScript.value = HTMLDencode(oActiveEl.selectedScript);
    }
    SelectField( 'name' ) ;
    window.top.toThisHelpPage("application_module_form_info_advance_tab");
}

// 点击返回
function Ok(){
	
	oEditor.FCKUndo.SaveUndoStep() ;
	//检查内容是否完成正确
	if(!ev_check())
		return false;
	
	oActiveEl = getCreateNamedElement();
	return true;
}
//检查内容是否完成正确
function ev_check(){
   if(temp.name.value==''){
 	 alert("{*[page.name.notexist]*}");
	  return false;
   }else if(temp.formId.value=="none"){
	   alert("{*[page.workflow.form.select]*}");
	   return false;
   }else{
	   var names = document.getElementsByName("name");
	   //名称为空校验
	   for(var i=0;i<names.length;i++){
		   var value = names[i].value;
		   if(value == ''){
			   alert("{*[page.name.notexist]*}");
			   return false;
		   }
	   }
	   //同名校验
	   var allowSameName = document.getElementById("allowSameName").checked;
	   if(!allowSameName){
		   for(var i=0;i<names.length;i++){
			   for(var j=0;j<names.length;j++){
				   if(names[i].value == names[j].value && i != j){
					   alert("{*[page.exist.same.name]*}");
					   return false;
				   }
			   }
		   }
	   }
   }
   return true;
}

function checkStartChar(value){
	return IsDigit(value,"{*[page.name.startingit]*}");
}
function IsDigit(s,msg){
	var text = s.substring(0,1);
	var patrn=/^[0-9]{1}$/; 
	if(s=="action"){
	   	alert("action为关键字，不能作为字段名！");
	   	return true;
   	}else if (patrn.exec(text)){
		alert(msg);
		return true;
	}
	return false;
} 
var instance = '<ww:property value="#parameters.application" />';
var module = '<ww:property value="#parameters.moduleid" />';
var currentFormId = '<ww:property value="#parameters.formid" />'

var rowIndex = 1;
var getTag = function(data) { return '{*[Tab]*}:' };
var getName = function(data) {
  	return '<input type="text" name="name" id="name" value="' + HTMLDencode(data.name) + '" onchange="checkStartChar(this.value);" size="15"/>';
};
var getForm = function(data) { 
	var s =''; 
	s +='<select id="formId'+ rowIndex +'" name="formId">';
	s +='<option value="'+data.formId+'" selected></option>';
	s +='</select>';
	return s; 
}; 
var getRefresh = function(data) {
	var s = '<input id="refresh'+rowIndex+'" type="checkbox" name="refreshOnChanged" value="true"';
	if (data.refreshOnChanged) {
		s+= ' checked';
	}
	s += '>';
	return s; 
};
var getRecalculate = function(data) {
	var s = '<input id="recalculate'+rowIndex+'" type="checkbox" name="calculateOnRefresh" value="true"';
	if (data.calculateOnRefresh) {
		s+= ' checked';
	}
	s += '>';
	return s; 
};
var getHiddenScript = function(data) {
	var s = '<input type="button" value="{*[Open]*}" onclick="showDialog(\'script'+rowIndex+'\')"/>';
	s += '<textarea id="script'+rowIndex+'" name="hiddenScript" style="display:none"/>'+HTMLDencode(data.hiddenScript)+'</textarea>';
	return s;
};
var getHiddenPrintScript = function(data) {
	var s = '<input type="button" value="{*[Open]*}" onclick="showDialog(\'hscript'+rowIndex+'\')"/>';
	s += '<textarea id="hscript'+rowIndex+'" name="hiddenPrintScript" style="display:none"/>'+HTMLDencode(data.hiddenPrintScript)+'</textarea>';
	return s;
};
var getDelete = function(data) {
  	var s = '<input type="button" value="{*[Delete]*}" onclick="delRow(tb, this.parentNode.parentNode)"/>';
  	rowIndex ++;
  	return s;
};

//DWRUtil.setEscapeHtml(false);

// 根据数据增加行
function addRows(datas,relStr) {
	var cellFuncs ="";
	
	cellFuncs = [getTag, getName, getForm, getRefresh, getRecalculate, getHiddenScript,getHiddenPrintScript,getDelete];
	
	var rowdatas = datas;
	if (!datas) {
		var data="";
		data = {name:'', formId:'', refreshOnChanged:'', calculateOnRefresh:'', hiddenScript:'',hiddenPrintScript:''};
		rowdatas = [data];
	}
	DWRUtil.addRows("tb", rowdatas, cellFuncs);
	ApplicationUtil.getFormByModule(module, instance, function(options) {
		addOptions("formId", options);
	});

	resize();
}

// 删除一行
function delRow(id, row) {
	var elem = $(id);
	if (elem) {
		elem.deleteRow(row.rowIndex);
		rowIndex --;
	}
	resize();
}

// 增加element options
function addOptions(elemName, options) {
	delete options[currentFormId]; // 移除当前表单选项
	var elems = document.getElementsByName(elemName);
	for (var i=0; i<elems.length; i++) {
		var defVal = elems[i].value;
		DWRUtil.removeAllOptions(elems[i].id);
		DWRUtil.addOptions(elems[i].id, options);
		DWRUtil.setValue(elems[i].id, defVal);
	}
}

// 根据mapping str获取data array
	function parseRelStr(str) {
		var obj = eval(str);
		if (obj instanceof Array) {
			return obj;
		} else {
			return new Array();	
		}
	}

// 根据页面内容生成关系语句
function createRelStr() {
	var names = document.getElementsByName("name");
	var formIds = document.getElementsByName("formId");
	var scripts = document.getElementsByName("hiddenScript");
	var refreshs = document.getElementsByName("refreshOnChanged");
	var recalculates = document.getElementsByName("calculateOnRefresh");
	var pintscripts  = document.getElementsByName("hiddenPrintScript");
	var str = '[';
	for (var i=0;i<names.length;i++) {
		if (names[i].value != '' && formIds[i].value != '') {
			str += '{';
			str += names[i].name +':\''+ HTMLEncode(names[i].value) + '\',';
			str += formIds[i].name +':\''+ formIds[i].value + '\',';
			str += scripts[i].name +':\''+ HTMLEncode(scripts[i].value) + '\',';
			str += pintscripts[i].name +':\''+ HTMLEncode(pintscripts[i].value) + '\',';
			str += refreshs[i].name +':'+ (refreshs[i].checked ? true : false) + ',';
			str += recalculates[i].name +':'+ (recalculates[i].checked ? true : false) + '';
			str += '},';
		}
	}
	str = str.substring(0, str.length - 1);
	str += ']';
	return  str;
}

function showDialog(elId) {
	var el = $(elId);
	var width = "510";
	var height = "290";
	var url = "hiddenscript.jsp";
	var rtn = "";
	
	if (el) {
		//alert(el.value);
		rtn = showModalDialog(url, el.value, "dialogWidth:" + width + "px;dialogHeight:" + height+"px;help:no;scroll:auto;status:no");
		if (rtn) {
			el.value = rtn;
		}
		if (rtn == '') {
			el.value = rtn;
		}
	}	
	
}

function show_OpenAll(s){
	var showSpan=document.getElementById("showSpan");
	
	if(s==0){
		document.getElementById("open_All").checked=false;
		document.getElementById("open_All").value="false";
		showSpan.style.display="none";
	}else if(s==1){
		document.getElementById("open_All").checked=true;
		document.getElementById("open_All").value="true";
		showSpan.style.display="inline";
	}else{
		document.getElementById("open_All").checked=false;
		document.getElementById("open_All").value="false";
		showSpan.style.display="none";
	}

}

function changeValue(){
	var open_All=document.getElementById("open_All");
	
	if(open_All.checked==true){
		open_All.checked=true;
		open_All.value="true";
	}else{
		open_All.checked=false;
		open_All.value="false";
	}
}

</script>
 
</HEAD>

<BODY onload="InitDocument()">

<form name="temp">

<table border=0 cellpadding=3 cellspacing=0>
<tr align=center>
	<td class="card" onclick="cardClick(1)" id="card1">{*[Basic]*}</td>
	<td width=2></td>
	<td class="card" onclick="cardClick(2)" id="card2">{*[tab.selected.script]*}</td>
	<td width=2></td>
</tr>

<tr>
<td valign="middle" colspan="13" bgcolor="#ffffff" align="center" width="100%">
	<!-- *************************** content1 ********************************* -->
	<table border=1 cellpadding=3 cellspacing=1 class="content" width="100%" id="content1">
		<tr>
		<td align="left">
			{*[ShowMode]*}:
			<select name="showMode" id="showMode" onchange="show_OpenAll(this.value)">
				<option value="0" selected>{*[Normal]*}</option>
				<option value="1">{*[Collapse]*}</option>
			</select>
			<span id="showSpan" style="display:none">
			{*[IsOpenAll]*}
			<input type="checkbox" name="openAll" onclick="changeValue()" id="open_All" value="true"/>
			</span>
		</td>
		<td>
			{*[is.allow.same.name]*}:
			<input type="checkbox" id="allowSameName" checked="checked"/>
		</td>
		<td align="right">
			<input type="button" value="{*[Add]*}" onclick="addRows()"/> 
		</td></tr>
		<tr>
		<td colspan="3">
			<table border=0 class="content">
				<tbody id="tb">
				<tr>
					<td>&nbsp;</td>
					<td>{*[Name]*}</td>
					<td>{*[Form]*}</td>
					<td>{*[Refresh]*}</td>
					<td>{*[Recalculate]*}</td>
					<td >{*[Hidden_Script]*}</td>
					<td>{*[Hidden_Print_Script]*}</td>
					<td>&nbsp;</td>
				</tr>
				</tbody>
			</table>
		</td>
		</tr>
	</table>
	<!-- *************************** content2 ********************************* -->		
	<table border=1 cellpadding=3 cellspacing=1 class="content" id="content2" style="width:100%">
	<tr>
		<td><textarea id="selectedScriptId" name="selectedScript" style="width:500" rows="10"></textarea>
		<button style="border:0px;cursor: pointer;" onclick="openIscriptEditor('selectedScript','{*[Script]*}{*[Editor]*}','{*[Selected_Script]*}','name','{*[Save]*}{*[Success]*}');"><img alt="Open with IscriptEditor" src="<ww:url value='/resource/image/editor.png' />"/></button>
		</td>
	</tr>
	</table>
	
	</td>
</tr>
</form>
<script language=javascript>
cardClick(1);
</script>
</BODY>
</o:MultiLanguage>
</HTML>
